package com.zrgg.futuwen.base.bottomsheet;

import android.app.Dialog;
import android.content.Context;
import android.graphics.Color;
import android.graphics.drawable.Drawable;
import android.support.annotation.LayoutRes;
import android.view.Display;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.Window;
import android.view.WindowManager;
import android.widget.LinearLayout;
import android.widget.LinearLayout.LayoutParams;
import android.widget.TextView;

import com.phillipcalvin.iconbutton.IconButton;
import com.zrgg.futuwen.R;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * 创建人：wangqingbang
 */
public class GCActionSheetHorizontalDialog {
    private Context context;
    private Dialog dialog;
    private TextView txt_title;
    private TextView txt_cancel;
    private LinearLayout lLayout_content;
    private boolean showTitle = false;
    private List<SheetItem> sheetItemList;
    private Display display;
    private List<Integer> listIds;


    private ISheetDialogClickListener mISheetDialogClickListener;

    public GCActionSheetHorizontalDialog(Context context) {
        this.context = context;
        try {
            mISheetDialogClickListener = (ISheetDialogClickListener) context;
        } catch (Exception e) {
            e.printStackTrace();
        }
        WindowManager windowManager = (WindowManager) context
                .getSystemService(Context.WINDOW_SERVICE);
        display = windowManager.getDefaultDisplay();
    }

    public GCActionSheetHorizontalDialog builder() {
        // 获取Dialog布局
        View view = LayoutInflater.from(context).inflate(
                R.layout.view_actionsheet_horizontal, null);

        // 设置Dialog最小宽度为屏幕宽度
        view.setMinimumWidth(display.getWidth());

        // 获取自定义Dialog布局中的控件
        lLayout_content = (LinearLayout) view
                .findViewById(R.id.lLayout_content);
        txt_title = (TextView) view.findViewById(R.id.txt_title);
        txt_cancel = (TextView) view.findViewById(R.id.txt_cancel);
        txt_cancel.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                dialog.dismiss();
            }
        });

        // 定义Dialog布局和参数
        dialog = new Dialog(context, R.style.ActionSheetDialogStyle);
        dialog.setContentView(view);
        Window dialogWindow = dialog.getWindow();
        dialogWindow.setGravity(Gravity.LEFT | Gravity.BOTTOM);
        WindowManager.LayoutParams lp = dialogWindow.getAttributes();
        lp.x = 0;
        lp.y = 0;
        dialogWindow.setAttributes(lp);

        return this;
    }

    public GCActionSheetHorizontalDialog setTitle(String title) {
        showTitle = true;
        txt_title.setVisibility(View.VISIBLE);
        txt_title.setText(title);
        return this;
    }

    public GCActionSheetHorizontalDialog setCancelable(boolean cancel) {
        dialog.setCancelable(cancel);
        return this;
    }

    public GCActionSheetHorizontalDialog setCanceledOnTouchOutside(boolean cancel) {
        dialog.setCanceledOnTouchOutside(cancel);
        return this;
    }

    public GCActionSheetHorizontalDialog addSheetItem(@LayoutRes int layout, Integer... ids) {
        if (sheetItemList == null) {
            sheetItemList = new ArrayList<SheetItem>();
        }
        sheetItemList.clear();
        SheetItem item = new SheetItem("", SheetItemColor.ALBUM, 0, null);
        item.setLayoutId(layout, ids);
        sheetItemList.add(item);
        return this;
    }


    /**
     * 设置条目布局
     */
    private void setSheetItems() {
        if (sheetItemList == null || sheetItemList.size() <= 0) {
            return;
        }

        int size = sheetItemList.size();

        // 循环添加条目
        for (int i = 1; i <= size; i++) {

            final int index = i;
            SheetItem sheetItem = sheetItemList.get(i - 1);


            if (sheetItem.getLayoutId() != -1) {
                View viewGroup = View.inflate(context, sheetItem.getLayoutId(), null);
                lLayout_content.addView(viewGroup);

                for (int n = 0; n < listIds.size(); n++) {
                    viewGroup.findViewById(listIds.get(n)).setOnClickListener(new OnClickListener() {
                        @Override
                        public void onClick(View v) {
                            mISheetDialogClickListener.clickLayoutId(v.getId());
                        }
                    });
                }

                return;
            }


            String strItem = sheetItem.name;
            SheetItemColor color = sheetItem.color;
            final OnSheetItemClickListener listener = (OnSheetItemClickListener) sheetItem.itemClickListener;

            IconButton textView = new IconButton(context);
            textView.setText(strItem);
            textView.setTextSize(15);
            textView.setGravity(Gravity.BOTTOM);
            LayoutParams params = (LayoutParams) lLayout_content
                    .getLayoutParams();
//			params.width =  ;
            textView.setLayoutParams(params);

            // 背景图片
            if (size == 1) {
                if (showTitle) {
                    textView.setBackgroundResource(R.drawable.btn_bg_n_shape);
                } else {
                    textView.setBackgroundResource(R.drawable.btn_bg_n_shape);
                }
            } else {
                if (showTitle) {
                    if (i >= 1 && i < size) {
                        textView.setBackgroundResource(R.drawable.btn_bg_n_shape);
                    } else {
                        textView.setBackgroundResource(R.drawable.btn_bg_n_shape);
                    }
                } else {
                    if (i == 1) {
                        textView.setBackgroundResource(R.drawable.btn_bg_n_shape);
                    } else if (i < size) {
                        textView.setBackgroundResource(R.drawable.btn_bg_n_shape);
                    } else {
                        textView.setBackgroundResource(R.drawable.btn_bg_n_shape);
                    }
                }
            }

            // 字体颜色
            if (color == null) {
                textView.setTextColor(Color.parseColor(SheetItemColor.CANCEL
                        .getName()));
            } else {
                textView.setTextColor(Color.parseColor(color.getName()));
            }

            // 高度
            float scale = context.getResources().getDisplayMetrics().density;
            int height = (int) (100 * scale + 0.5f);
            textView.setLayoutParams(new LayoutParams(
                    LayoutParams.MATCH_PARENT, height));

            // 点击事件
            textView.setOnClickListener(new OnClickListener() {
                @Override
                public void onClick(View v) {
                    listener.onClick(index);
                    dialog.dismiss();
                }
            });
            if (sheetItem.drawableId != 0) {
                Drawable drawable = context.getResources().getDrawable(sheetItem.drawableId);
                drawable.setBounds(drawable.getMinimumWidth(), drawable.getMinimumHeight(), drawable.getMinimumWidth(), drawable.getMinimumHeight());
                textView.setCompoundDrawablesWithIntrinsicBounds(null, drawable, null, null);
                textView.setIconPadding(10);
            }
            lLayout_content.addView(textView);
        }
    }

    public void show() {
        setSheetItems();
        dialog.show();
    }


    public interface OnSheetItemClickListener {
        void onClick(int which);
    }

    public class SheetItem {
        String name;
        OnSheetItemClickListener itemClickListener;
        SheetItemColor color;
        int drawableId;

        public int getLayoutId() {
            return layoutId;
        }

        public void setLayoutId(int layoutId, Integer ids[]) {
            this.layoutId = layoutId;
            listIds = Arrays.asList(ids);
        }

        int layoutId = -1;


        public SheetItem(String name, SheetItemColor color,
                         int drawableId, OnSheetItemClickListener itemClickListener) {
            this.name = name;
            this.color = color;
            this.drawableId = drawableId;
            this.itemClickListener = itemClickListener;
        }
    }

    public enum SheetItemColor {
        CANCEL("#000000"), ALBUM("#666666"), PHOTO("#000000"), DEFAULT("#000000");

        private String name;

        private SheetItemColor(String name) {
            this.name = name;
        }

        public String getName() {
            return name;
        }

        public void setName(String name) {
            this.name = name;
        }
    }

    public void dialogDismiss() {
        if (dialog != null)
            dialog.dismiss();
    }


    public interface ISheetDialogClickListener {
        void clickLayoutId(int id);
    }

}
